import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:share_fit/app/components/back_btn_icon.dart';
import 'package:share_fit/app/controllers/device/permission/permission_const.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/tools/color_const.dart';
import 'package:share_fit/app/ute/uteped_const.dart';
import 'package:share_fit/generated/locales.g.dart';

import 'permission_logic.dart';

class PermissionPage extends StatelessWidget {
  PermissionPage({super.key});

  final logic = Get.put(PermissionLogic());
  final state = Get.find<PermissionLogic>().state;

  @override
  Widget build(BuildContext context) {

    List permissionList = [
      {"name": LocaleKeys.permission_notification.ts, UTEPedConst.keyType: UTEPedConst.keySystem, UTEPedConst.keyPermission: "notification"},
      {"name": LocaleKeys.permission_app_notification.ts, UTEPedConst.keyType: UTEPedConst.keySystem, UTEPedConst.keyPermission:"app_notification"},
      {"name": LocaleKeys.permission_contacts.ts, UTEPedConst.keyType: UTEPedConst.keyPermission, UTEPedConst.keyPermission:"android.permission.READ_CONTACTS"},
      {"name": LocaleKeys.permission_phone.ts, UTEPedConst.keyType: UTEPedConst.keyPermission, UTEPedConst.keyPermission:"android.permission.READ_PHONE_STATE"},
      {"name": LocaleKeys.permission_sms.ts, UTEPedConst.keyType: UTEPedConst.keyPermission, UTEPedConst.keyPermission:UTEPedConst.permissionSms},
      {"name": LocaleKeys.permission_call_logs.ts, UTEPedConst.keyType: UTEPedConst.keyPermission, UTEPedConst.keyPermission:"android.permission.READ_CALL_LOG"},
      {"name": LocaleKeys.permission_camera.ts, UTEPedConst.keyType: UTEPedConst.keyPermission, UTEPedConst.keyPermission:"android.permission.CAMERA"},
      {"name": LocaleKeys.permission_location.ts, UTEPedConst.keyType: UTEPedConst.keyPermission, UTEPedConst.keyPermission:UTEPedConst.permissionLocation},
      {"name": LocaleKeys.permission_storage.ts, UTEPedConst.keyType: UTEPedConst.keyPermission, UTEPedConst.keyPermission:"android.permission.WRITE_EXTERNAL_STORAGE"},
    ];

    logic.queryPermissions(permissionList);

    return Scaffold(
      appBar: AppBar(
        title: Text(LocaleKeys.settings_permission.ts),
        centerTitle: true,
        leading: BackBtnIcon(onTap: () => Get.back()),
      ),
      body: Padding(
        padding: EdgeInsets.only(left: 43.w, right: 42.w),
        child: GetBuilder<PermissionLogic>(
            id: "list_container",
            init: logic,
            builder: (PermissionLogic controller) {
              return SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    SizedBox(
                      height: 80.h,
                    ),
                    Text(LocaleKeys.settings_permission_allowed.ts,
                        style: TextStyle(
                            fontSize: 42.sp, color: ColorConst.btnFontColor)),
                    SizedBox(
                      height: 17.h,
                    ),
                    Column(
                      children: state.authedItems.map(_buildListItem).toList(),
                    ),
                    SizedBox(
                      height: 40.h,
                    ),
                    Text(LocaleKeys.settings_permission_not_allowed.ts,
                        style: TextStyle(
                            fontSize: 42.sp, color: ColorConst.btnFontColor)),
                    SizedBox(
                      height: 17.h,
                    ),
                    Column(
                      children:
                      state.notAuthItems.map(_buildListItem).toList(),
                    ),
                  ],
                ),
              );
            }),
      ),
    );
  }

  Widget _buildListItem(permissionItem) {
    Widget widget = Container(
        padding: EdgeInsets.only(bottom: 38.h),
        child: Container(
          height: 176.w,
          padding: EdgeInsets.only(left: 60.w, right: 60.w),
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(18), color: Colors.white),
          child: Row(children: [
            // Image.asset(
            //   permissionItem[PermissionConst.keyIcon],
            //   width: 100.w,
            //   height: 100.w,
            // ),
            SizedBox(
              width: 40.w,
            ),
            Text(
              permissionItem[UTEPedConst.keyName],
              style: TextStyle(fontSize: 42.sp, color: const Color(0xFF2B2B2B)),
            ),
            Expanded(child: Container()),
            InkWell(
              child: Image.asset(
                logic.hasPermission(permissionItem[PermissionConst.keyPermission])
                    ? "assets/images/device/ic_circle_checked.png"
                    : "assets/images/device/ic_circle_unchecked.png",
                width: 66.w,
                height: 66.w,
              ),
              onTap: () {
                logic.toPermissionPage(permissionItem[UTEPedConst.keyType], permissionItem[UTEPedConst.keyPermission]);
              },
            )
          ]),
        ));

    return widget;
  }
}
